using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwingEffect : MonoBehaviour
{
    public Vector3 offsetUp;
    public Vector3 offsetDown;

    [Range(0.1f, 10)]
    public float cycleTime;

    #region Public方法

    #endregion

    #region Private方法
    private IEnumerator Coroutine()
    {
        float endTime;
        float lerp;
        Vector3 startPostion = transform.position;
        Vector3 endPosition = transform.position - offsetDown;
        Vector3 newPosition = Vector3.zero;
        endTime = Time.time + cycleTime / 2;
        lerp = 0;
        while (Time.time < endTime)
        {
            yield return null;
            lerp += Time.deltaTime / (cycleTime / 2);
            newPosition.x = Mathf.Lerp(startPostion.x, endPosition.x, lerp);
            newPosition.y = Mathf.Lerp(startPostion.y, endPosition.y, lerp);
            newPosition.z = Mathf.Lerp(startPostion.z, endPosition.z, lerp);
            transform.localPosition = newPosition;
        }
        int factor = 1;
        while (true)
        {
            lerp = 0;
            endTime = Time.time + cycleTime;
            startPostion = transform.position;
            endPosition = transform.position + (offsetUp + offsetDown) * factor;
            while (Time.time < endTime)
            {
                yield return null;
                lerp += Time.deltaTime / cycleTime;
                newPosition.x = Mathf.Lerp(startPostion.x, endPosition.x, lerp);
                newPosition.y = Mathf.Lerp(startPostion.y, endPosition.y, lerp);
                newPosition.z = Mathf.Lerp(startPostion.z, endPosition.z, lerp);
                transform.localPosition = newPosition;
            }
            factor *= -1;
        }
    }
    #endregion

    #region Unity方法
    private void Awake()
    {
        StartCoroutine(Coroutine());
    }
    #endregion
}